<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h2>注册页</h2>
    <form onsubmit="newuserinfo(this);return false;" autocomplete="off">
        <input type="text" name="username" oninput="cacheInput(this);" placeholder="用户名" required>
        <input type="password" name="passwd" oninput="cacheInput(this);" placeholder="密码" required>
        <input type="submit" value="注册">
    </form>
    <input type="button" onclick = history.back() value="返回登录">
    <script>
        let newinfo = {
          username : '',
          passwd : ''
        }
    
        function cacheInput (t) {
          if (t.name === 'username') {
            newinfo.username = t.value.trim()
            console.log(newinfo.username)
          } else if (t.name === 'passwd') {
            newinfo.passwd = t.value
            console.log(newinfo.passwd)
            console.log(JSON.stringify(newinfo))
          }
        }

        function newuserinfo(t) {
          console.log(t);
          if (newinfo.username === '' || newinfo.passwd === '') {
            return
          }
    
          fetch('/newdata', {
            method : 'POST',
            headers : {
              'content-type' : 'application/json'
            },
            body : JSON.stringify(newinfo)
          }).then(res => res.json()).catch(err => {
            alert(err.message)
          }).then(response => {
              console.log(response)
            if(response.status === '注册成功'){
              alert('注册成功')
              // location.href = '/login';
              console.log(response)
            }else{
              alert('注册失败')
            }
          });

        }
    </script>
</body>
</html>